package net.adetech.crj.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.entity.Appointment;
import net.adetech.crj.entity.User;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.framework.entity.ServiceResponse;
import net.adetech.framework.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class AppointmentServiceController {



    @Autowired
    ServiceUtil serviceUtil;





    @RequestMapping(value = "/appointment/monthList",produces = "application/json")
    public String getAppointmentDayList(long hallId,String serviceType)
    {
        if("M".equals(serviceType))
        {
            serviceType = "B";
        }
        ServiceResponse response = serviceUtil.callService("appointment.getMonthList","start",new Date(),"offset",30,"hallId",hallId,"serviceType",serviceType);
        String monthList = (String)response.getResults().get("monthList");
        //JSONArray monthArray = monthListService.getDayArray(new Date(),180);
        return monthList;
    }


    @RequestMapping(value = "/appointment/add",produces = "application/json")
    public String addAppointment(Appointment appointment, HttpSession session)
    {
        User user = (User)session.getAttribute("currentUser");
        ServiceResponse response = serviceUtil.callService("appointment.insert","appointment",appointment,"user",user);
        return JSON.toJSONString(response.getResults());
    }

    @RequestMapping(value = "/appointment/group/add",produces = "application/json")
    public String addGroupAppointment(Appointment appointment, HttpSession session)
    {
        User user = (User)session.getAttribute("currentUser");
        ServiceResponse response = serviceUtil.callService("appointment.insert","appointment",appointment,"user",user);
        return JSON.toJSONString(response.getResults());
    }

    @RequestMapping(value = "/appointment/list",produces = "application/json")
    public String appointmentList(Appointment appointment, HttpSession session)
    {
        User user = (User)session.getAttribute("currentUser");
        String applyUserId = user.getIdNumber();
        ServiceResponse response = serviceUtil.callService("appointment.getListByApplyUserId","applyUserId",applyUserId);
        return JSON.toJSONString(response.getResults().get("appointmentList"));
    }



    @RequestMapping(value = "/appointment/check",produces = "application/json")
    public String check(String idNumber)
    {
        JSONObject result = new JSONObject();

        ServiceResponse serviceResponse = serviceUtil.callService("appointment.getByIdNumber","idNumber",idNumber);

        Appointment appointment = (Appointment) serviceResponse.get("appointment");
        if (appointment!=null&&"1".equals(appointment.getState()))
        {
            result.put("result",true);
        }
        else {
            result.put("result",false);
        }
        return  result.toJSONString();


    }

    @RequestMapping(value = "/appointment/list/current",produces = "application/json")
    public String getAppointmentListByIdNumber(String idNumber,String serviceType)
    {
        ServiceResponse serviceResponse = serviceUtil.callService("appointment.getCurrentListByIdNumber","idNumber",idNumber,"serviceType",serviceType);
        List<Appointment> appointmentList  = (List<Appointment>)serviceResponse.get("appointmentList");
        List<Appointment> resultList = new LinkedList<Appointment>();
        Set<String> idNumberSet = new HashSet<String>();
        for (Appointment a:appointmentList
             ) {
            if (a.getIdNumber().equals(idNumber))
            {
                resultList.add(a);
                idNumberSet.add(a.getIdNumber());
                break;
            }
        }

        for (Appointment a:appointmentList
                ) {
            if (!idNumberSet.contains(a.getIdNumber()))
            {
                resultList.add(a);
                idNumberSet.add(a.getIdNumber());
            }
        }


        return JSON.toJSONString(resultList);
    }

    @RequestMapping(value = "/appointment/use",produces = "application/json")
    public String useAppointment(String idList)
    {

        ServiceResponse response = serviceUtil.callService("appointment.use","idList",idList);

        return "{result:'ok'}";

        //return JSON.toJSONString(resultList);
    }


    @RequestMapping(value = "/appointment/cancel",produces = "application/json")
    public String cancelAppointment(String idList)
    {

        ServiceResponse response = serviceUtil.callService("appointment.cancel","idList",idList);

        return "{result:'ok'}";
        }



}
